const baseURL = 'https://ttapi.mzhapi.com/';
// const baseURL="http://192.168.16.132:9999/api";
// const baseURL="http://192.168.16.184:9999/api";
var exceptionAddrArr = ['/user/login'];
function CreateHeader(url, type) {
    let header = {};
    if (type == 'POST_PARAMS') {
        header = {
            'content-type': 'application/x-www-form-urlencoded'
        };
    } else {
        header = {
            'content-type': 'application/json'
        };
    }
    if (exceptionAddrArr.indexOf(url) == -1) {
        //排除请求的地址不需要token的地址
        let token = uni.getStorageSync('token');
        header['Token'] = token;
    }
    header['Channel-id'] = 'dciwpk';
    let newHannel = uni.getStorageSync('channelid');
    let linkid = uni.getStorageSync('linkid');
    if (newHannel) {
        header['Channel-id'] = newHannel;
       
    }
	 header['link-id'] = linkid;
	// #ifdef MP-WEIXIN
    header['appid'] = uni.getAccountInfoSync().miniProgram.appId;
	// #endif
	// #ifdef APP
	header['appid'] ='tt6432128ffb01360501';
	// #endif
    return header;
}
function request(method, url, data, ping = false) {
    return new Promise(function (resolve, reject) {
        console.log('data', method);
        let header = CreateHeader(url, method);
        var newurl = '';
        if (ping) {
            //如果是要拼地址，例如“url/参数值”
            newurl = baseURL + url + '/' + data;
        } else {
            newurl = baseURL + url;
        }
        uni.request({
            url: newurl,
            method: method,
            data: method === 'POST' ? JSON.stringify(data) : data,
            header: header,
            success(res) {
                //resolve(res);
                //请求成功
                //判断状态码---errCode状态根据后端定义来判断
                if (res.statusCode == 200) {
                    resolve(res);
                } else {
                    //其他异常
                    reject('运行时错误,请稍后再试');
                }
            },
            fail(err) {
                //请求失败
                reject(err);
            }
        });
    });
}
function request2(method, url, data, ping = false) {
    return new Promise(function (resolve, reject) {
        var newurl = '';
        if (ping) {
            //如果是要拼地址，例如“url/参数值”
            newurl = baseURL + url + '/' + data;
        } else {
            newurl = baseURL + url;
        }
        uni.request({
            url: newurl,
            method: method,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
                //resolve(res);
                //请求成功
                //判断状态码---errCode状态根据后端定义来判断
                if (res.statusCode == 200) {
                    resolve(res);
                } else {
                    //其他异常
                    reject('运行时错误,请稍后再试');
                }
            },
            fail(err) {
                //请求失败
                reject(err);
            }
        });
    });
}
function getr(url, data, ping) {
    return request('GET', url, data, ping);
}
function postr(url, data) {
    return request('POST', url, data);
}
function postr2(url, data) {
    return request2('POST', url, data);
}
function deleter(url, data, ping) {
    return request('DELETE', url, data, ping);
}
function putr(url, data, ping) {
    return request('PUT', url, data, ping);
}
module.exports = {
    get: getr,
    post: postr,
    post2: postr2,
    delete: deleter,
    put: putr,
    baseurl: baseURL
};
